package juc.atomic;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceTest {

    public static void main(String[] args) {
        AtomicReference<Simple> atomic = new AtomicReference<>(new Simple("Alex", 21));
        System.out.println(atomic.get());
        boolean result = atomic.compareAndSet(new Simple("sdfs", 22), new Simple("sdfs", 23));
        System.out.println(result);

        //***************
        JButton button = new JButton();
        //default
        final AtomicReference<Simple> s = new AtomicReference<>(new Simple("test", 12));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //invoke result service
                //s=new Simple("sdf",24);
                s.set(new Simple("qwe", 23));
            }
        });
    }

    static class ObjectWrap<T> {
        private T t;

        public ObjectWrap(T t) {
            this.t = t;
        }

        public T get() {
            return this.t;
        }

        public void set(T t) {
            this.t = t;
        }
    }

    static class Simple {
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public Simple(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
}
